package base.producer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

/**
 * 同步生产者
 */
@Slf4j
public class SyncProducer {
    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        // 生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("syncProducer");
        // Namesrv 地址
        producer.setNamesrvAddr("192.168.147.151:9876;192.168.147.152:9876");

        producer.start();

        for (int i = 0; i < 10; i++) {
            StringBuffer sb = new StringBuffer();
            sb.append("Hello")
                    .append(i);

            Message message = new Message("hello", "TagA", sb.toString().getBytes(StandardCharsets.UTF_8));

            SendResult result = producer.send(message);

            log.info(result.toString());

//            TimeUnit.MILLISECONDS.sleep(10);
        }
        // 关闭
        producer.shutdown();


    }
}
